package com.punai.config.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.punai.config.constants.PnConfigConstants;
import com.punai.config.service.ConfigGenerateCodeService;
import com.punai.config.service.ConfigProjectFeatureService;
import com.punai.config.sqlitedao.ConfigFeatureElementRepository;
import com.punai.config.sqlitedao.ConfigFormatLinkFeatureRepository;
import com.punai.config.sqlitedomain.ConfigFeatureElement;
import com.punai.config.sqlitedomain.ConfigFormatLinkElement;
import com.punai.config.sqlitedomain.vo.ConfigFeatureElementVo;
import io.netty.util.internal.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ConfigProjectFeatureServiceImpl implements ConfigProjectFeatureService {

    @Autowired
    private ConfigFeatureElementRepository configFeatureElementRepository;
    @Autowired
    private ConfigFormatLinkFeatureRepository configFormatLinkFeatureRepository;
    @Autowired
    private ConfigGenerateCodeService configGenerateCodeService;

    @Override
    public Page<ConfigFeatureElement> selectList(ConfigFeatureElementVo vo) throws ParseException {
        Integer pageSize = vo.getPageSize();
        Integer pageNumber = vo.getPageNumber();
        PageRequest pageRequest =  PageRequest.of(pageNumber, pageSize);
        Map<String,Object> paramMap = new HashMap<>();
        String formatId = vo.getFormatId();
        paramMap.put("formatCategoryId",formatId);
        paramMap.put("featureType",vo.getFeatureType());
        Page<ConfigFeatureElement> elementPage = configFeatureElementRepository.findPageByformat(paramMap, pageRequest);
        return elementPage;
    }

    @Override
    public int add(ConfigFeatureElement configFeatureElement) throws ParseException {
        ConfigFeatureElement save=null;
        ConfigFeatureElement findByCode = configFeatureElementRepository.findByCode(configFeatureElement.getCode());
        Boolean isQDGF = configFeatureElement.getQDGF();
        if(findByCode!=null){
            if(StringUtil.isNullOrEmpty(configFeatureElement.getId())){
                return 0;
            }
            if(!configFeatureElement.getId().equals(findByCode.getId())){
                return 0;
            }
        }
        if(configFeatureElement.getType().equals("select")||"combox".equals(configFeatureElement.getType())){
            if(isQDGF){
                configFeatureElement.setCalculateCaliberType("8888");
            }else {
                configFeatureElement.setCalculateCaliberType(null);
            }
        }
        if(StrUtil.isNotBlank(configFeatureElement.getId())){
            configFeatureElementRepository.update(configFeatureElement);
        }else{
            String formatId = configFeatureElement.getFormatId();
            save=configFeatureElementRepository.save(configFeatureElement);
            ConfigFormatLinkElement formatLinkFeature = new ConfigFormatLinkElement();
            formatLinkFeature.setFeatureId(save.getId());
            formatLinkFeature.setFormatcategoryId(formatId);
            configFormatLinkFeatureRepository.save(formatLinkFeature);
        }
        return 1;
    }

    @Override
    public ConfigFeatureElement getInfo(String id) throws ParseException {
        ConfigFeatureElement one = configFeatureElementRepository.findOne(id);
        if(one.getCalculateCaliberType()!=null && one.getCalculateCaliberType().equals("8888")){
            one.setQDGF(true);
        }
        return one;
    }

    @Override
    public int deleteInfo(String id) {
        return configFeatureElementRepository.delete(id);
    }


    @Override
    public int copyOtherClassNode(String formatIds, String elementIds) throws ParseException {
        String[] copyids = elementIds.split(",");
        String[] formatCategoryIds = formatIds.split(",");
        Map<String, String> parentids=new HashMap<String, String>();
        for (String id : copyids) {
            ConfigFeatureElement findOne = this.getInfo(id);
            for (String formatid : formatCategoryIds) {
                findOne.setId(null);
                findOne.setCode(configGenerateCodeService.getNextCode(PnConfigConstants.CODE_TYPE.ELE_CODE));
                findOne.setPid(null);
                //根据PID是否为空，复制父类
                if(findOne.getParent()!=null){
                    if(parentids.containsKey(findOne.getParent().getId())) {
                        findOne.setPid(parentids.get(findOne.getParent().getId()));
                    }else{
                        String oldId=findOne.getParent().getId();
                        ConfigFeatureElement save = saveParentOfMethodCopyToFormatCategory(findOne.getParent(), formatid);
                        findOne.setPid(save.getId());
                        parentids.put(oldId,save.getId());
                    }
                }
                ConfigFeatureElement save = this.save(findOne, formatid);
                parentids.put(id,save.getId());
            }
        }
        return 1;
    }

    private ConfigFeatureElement saveParentOfMethodCopyToFormatCategory(ConfigFeatureElement thisOne, String formatid) throws ParseException {
        // 父为空
        if(thisOne.getParent()==null){
            thisOne.setId(null);
            thisOne.setCode(configGenerateCodeService.getNextCode(PnConfigConstants.CODE_TYPE.ELE_CODE));
            thisOne.setFormatId(formatid);
            ConfigFeatureElement save = this.save(thisOne, formatid);
            return save;
        }
        // 不为空
        ConfigFeatureElement parent = saveParentOfMethodCopyToFormatCategory(thisOne.getParent(),formatid);
        thisOne.setId(null);
        thisOne.setCode(configGenerateCodeService.getNextCode(PnConfigConstants.CODE_TYPE.ELE_CODE));
        thisOne.setPid(parent.getId());
        ConfigFeatureElement save = this.save(thisOne, formatid);
        return save;
    }

    private ConfigFeatureElement save(ConfigFeatureElement configFeatureElement, String formatid) throws ParseException {
        ConfigFeatureElement save=null;
        ConfigFeatureElement findByCode = configFeatureElementRepository.findByCode(configFeatureElement.getCode());
        if(findByCode!=null){
            if(StringUtil.isNullOrEmpty(configFeatureElement.getId())){
                return save;
            }
            if(!configFeatureElement.getId().equals(findByCode.getId())){
                return save;
            }
        }
        if(StrUtil.isNotBlank(configFeatureElement.getId())){
            configFeatureElementRepository.update(configFeatureElement);
        }else{
            save=configFeatureElementRepository.save(configFeatureElement);
            ConfigFormatLinkElement formatLinkFeature = new ConfigFormatLinkElement();
            formatLinkFeature.setFeatureId(save.getId());
            formatLinkFeature.setFormatcategoryId(formatid);
            configFormatLinkFeatureRepository.save(formatLinkFeature);
        }
        return save;

    }

    @Override
    public boolean configProjectFeatureService(String formatId) throws ParseException {
        Map<String,Object> paramMap = new HashMap<>();
        paramMap.put("formatCategoryId",formatId);
        paramMap.put("isCalculateCaliber","Y");
        List<ConfigFeatureElement> allOfFormat = configFeatureElementRepository.findAllOfFormat(paramMap);
        return CollUtil.isEmpty(allOfFormat);
    }


}
